﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace CommonLibraries.DeviceLibrary
{
    public class Compass: ICompass
    {
        #region ICompass Members
        private static Compass Instance = null;
        private static void CreateInstance()
        {
            if (Instance == null)
            {
                Instance = new Compass();
            }
        }
        public static Compass GetInstance()
        {
            if (Instance == null) CreateInstance();
            return Instance;
        }
        private Compass() 
        { }

        #region PrivateMembers
        private PowerStates _powerstate;
        private int _bearing;
        private int _pitch;
        private int _roll;
        private int _yaw;
        private float m_Angle;
        #endregion

        public PowerStates PowerState
        {
            get
            {
                int _powerstate = TGMS2.TGMS2_GetCompassPowerState();
                return (_powerstate == 1 ? PowerStates.On : PowerStates.Off);
            }
            set
            {
                int _powerstate;
                _powerstate = (value == PowerStates.On ? 1 : 0);
                TGMS2.TGMS2_SetCameraPowerState(_powerstate);
            }
        }

        public int Bearing
        {
            get
            {
                return _bearing;
            }
        }

        public int Pitch
        {
            get
            {
                return _pitch;
            }
        }

        public int Roll
        {
            get
            {
                return _roll;
            }
        }

        public int Yaw
        {
            get
            {
                return _yaw;
            }
        }

        public float Angle
        {
            get { return m_Angle; }
            set { m_Angle = value; }
        }

        public bool Calibrate()
        {
            int bcalibrate = TGMS2.TGMS2_SetCompassCalibration(1);
            return Convert.ToBoolean(bcalibrate);
        }


        public void TestCalibration()
        {
            string stemp = "TestCalibration \n";
            int retorno;
            float pangle = 0;
            uint dwGetCount = 3;

            //CalibrationData 
            float pfXgain = 0;
            float pfYgain = 0;
            int pdwXset_Dc = 0;
            int pdwXreset_Dc = 0;
            int pdwYset_Dc = 0;
            int pdwYreset_Dc = 0;
            uint dwCalibData = 1;

            DateTime aDate = new DateTime();
            retorno = TGMS2.TGMS2_SetCompassPowerState(1);
            retorno = TGMS2.TGMS2_GetCompassValue(ref pangle, dwGetCount);
            retorno = TGMS2.TGMS2_GetCompassCalibrationData(ref pfXgain, ref pfYgain, ref pdwXset_Dc, ref pdwXreset_Dc, ref pdwYset_Dc, ref pdwYreset_Dc, dwCalibData);

            stemp += "Calibration Data /////////////// \n";
            stemp += "  pfXgain->" + pfYgain.ToString() + " \n";
            stemp += "  pfYgain->" + pfYgain.ToString() + " \n";
            stemp += "  pdwXset_Dc->" + pdwXset_Dc.ToString() + " \n";
            stemp += "  pdwXreset_Dc->" + pdwXreset_Dc.ToString() + " \n";
            stemp += "  pdwYset_Dc->" + pdwYset_Dc.ToString() + " \n";
            stemp += "  pdwYreset_Dc->" + pdwYreset_Dc.ToString() + " \n";
            stemp += "CompassAngle Angle->" + pangle.ToString() + "\n";

            Angle = pangle;
            CreateDebug(stemp);
        }

        #endregion

        protected void CreateDebug(string debugData)
        {
            StreamWriter _a = File.AppendText(".\\DebugCompass.txt");
            _a.WriteLine("##############################");
            _a.WriteLine(System.DateTime.Now.ToShortDateString() + " " + System.DateTime.Now.ToShortTimeString());
            _a.WriteLine("##############################");
            _a.WriteLine(debugData);
            _a.Close();
            _a.Dispose();

        }


    }
}
